from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from common.models import BaseEnum, Site, BaseTag, BaseCategory, Region, CATEGORY_TAXONOMY
from django.contrib.postgres.search import SearchVector,SearchVectorField

from uuslug import uuslug


class Author(models.Model):
    name = models.CharField(max_length=256)
    slug = models.SlugField(max_length=256, blank=True)
    country = models.ForeignKey(Region, models.CASCADE)
    introduction = models.TextField(default='')
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        self.slug = uuslug(self.name, instance=self)
        super(Author, self).save(*args, **kwargs)


class Tag(BaseTag):
    pass


class Book(models.Model):
    title = models.CharField(max_length=128)
    sub_title = models.CharField(max_length=256, default='', blank=True)
    slug = models.SlugField(max_length=128, db_index=True)
    #hash_id = models.CharField(max_length=32)
    isbn = models.CharField(max_length=128, db_index=True)
    year = models.SmallIntegerField(default=0)
    summary = models.TextField(default='', blank=True)
    cover_url = models.CharField(max_length=2048)
    tags = models.ManyToManyField(Tag)
    authors = models.ManyToManyField(
        Author,
        through='BookAuthor',
        through_fields=('book', 'author'),
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        self.slug = uuslug(self.title, instance=self)
        super(Book, self).save(*args, **kwargs)


class AuthorType(BaseEnum):
    Author = 1
    Translator = 2


AUTHOR_TYPE = AuthorType.to_choices()


class BookAuthor(models.Model):
    book = models.ForeignKey(Book, models.CASCADE)
    author = models.ForeignKey(Author, models.CASCADE)
    sort_order = models.IntegerField(default=0)
    author_type = models.IntegerField(default=1, choices=AUTHOR_TYPE)
    created_at = models.DateTimeField(auto_now_add=True)


class BookSource(models.Model):
    book = models.ForeignKey(Book, models.CASCADE)
    site = models.ForeignKey(Site, models.CASCADE)
    cover_url = models.CharField(max_length=2048)
    url = models.CharField(max_length=1024)
    key_int = models.BigIntegerField(default=0, blank=True)
    key_char = models.CharField(max_length=1024, default='', blank=True)
    language = models.CharField(max_length=32)
    page_cnt = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)


class BookDownloadUrl(models.Model):
    book_source = models.ForeignKey(BookSource, models.CASCADE)
    file_format = models.CharField(max_length=32)
    file_size = models.CharField(max_length=32)
    download_url = models.CharField(max_length=1024)
    created_at = models.DateTimeField(auto_now_add=True)


class BookMeta(models.Model):
    book = models.ForeignKey(Book, models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)


class Category(MPTTModel):
    """
    分类
    """
    name = models.CharField(max_length=128)
    slug = models.CharField(max_length=128)
    taxonomy = models.CharField(max_length=32, choices=CATEGORY_TAXONOMY)
    created_at = models.DateTimeField(auto_now_add=True)
    parent = TreeForeignKey('self', models.SET_NULL, null=True, blank=True,
                            related_name='children', db_index=True)
    sort_order = models.IntegerField(default=0)

    class MPTTMeta:
        order_insertion_by = ['sort_order', 'name']


class Chapter(models.Model):
    book = models.ForeignKey(Book, models.CASCADE)
    title = models.CharField(max_length=1024)
    parent = models.ForeignKey('self', models.CASCADE, null=True, blank=True,)
    sort_no = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)


class ChapterBody(models.Model):
    chapter = models.ForeignKey(Chapter, models.CASCADE)
    content = models.TextField()


class BookIndex(models.Model):
    book = models.OneToOneField(
        Book, on_delete=models.CASCADE, primary_key=True)
    index_at = models.DateTimeField(auto_now=True)
    tsv = SearchVectorField()
